package com.rzx.mybackend.controller.comment;

import com.rzx.mybackend.common.Result;
import com.rzx.mybackend.entity.GameComment;
import com.rzx.mybackend.service.CommentService;
import com.rzx.mybackend.service.UserService;
import com.rzx.mybackend.vo.CommentVO;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * CommentController
 *
 * @author renzhexian
 * @date 2025/7/22
 */
@RestController
@RequestMapping("/comment")
@RequiredArgsConstructor
public class CommentController {

    private static final Logger log = LoggerFactory.getLogger(CommentController.class);

    private final UserService userService;

    private final CommentService commentService;
    /**
     * 用户发布游戏评论
     * 消息队列异步写入
     * websocket实时推送
     * @param gameComment
     * @return
     */
    @PostMapping
    public Result putComment(@RequestBody GameComment gameComment) {

        Long userId = userService.selectUserIdByUserName(SecurityContextHolder.getContext().getAuthentication().getName());

        log.info("用户"+SecurityContextHolder.getContext().getAuthentication().getName()+"发布游戏评论");

        gameComment.setUserId(userId);

        gameComment.setCommentTime(new java.util.Date());

        commentService.putComment(gameComment);

        return Result.success();
    }

    @GetMapping("/getComment")
    public Result<List<CommentVO>> getComment(Long gameId) {
        // 根据游戏ID获取评论
        return Result.success(commentService.getComment(gameId));
    }
}
